package com.zhiyou.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.mapper.VideoMapper;
import com.zhiyou.pojo.DeleteVideoVo;
import com.zhiyou.pojo.QueryVideoVo;
import com.zhiyou.pojo.Video;
import com.zhiyou.pojo.VideoExample;
import com.zhiyou.pojo.VideoExample.Criteria;
import com.zhiyou.pojo.VideoSpeaker;


@Service
public class VideoService {

	@Autowired
	private VideoMapper videoMapper;
	
	public List<Video> selectAll(){
		
		return videoMapper.selectByExampleWithBLOBs(new VideoExample());
	}
    public List<VideoSpeaker> selectVideoByQueryVo(QueryVideoVo video){
		
		return videoMapper.selectVideoListByQueryVo(video);
	}
    public int countVideoByQueryVo(QueryVideoVo video){
    	return videoMapper.countVideoByQueryVo(video);
    }
    
    
	public void saveOrUpdate(Video video){
		
		if(null!=video.getId() && video.getId()!=0){
			videoMapper.updateByPrimaryKeyWithBLOBs(video);
		}else{
			video.setPlayNum(0);
			System.out.println("********"+video.getPlayNum());
			videoMapper.insert(video);
		}
	}
	
	public int delById(int id){
		return videoMapper.deleteByPrimaryKey(id);
	}
	
   public void delBatchByIds(Integer[] ids){
		
		VideoExample example = new VideoExample();
		
		 Criteria createCriteria = example.createCriteria();
		
		 List<Integer> list = new ArrayList<Integer>();
		 Collections.addAll(list, ids);
		 DeleteVideoVo vo = new DeleteVideoVo();
		/* createCriteria.andIdIn(list);*/
		 vo.setIds(list);
		/*return videoMapper.deleteByExample(example);*/
		 videoMapper.deleteBatchByIds(vo);
	}
	
	public Video selectById(int id){
		
		return videoMapper.selectByPrimaryKey(id);
	}
	public List<Video> selectVideoByCourseId(int courseId) {
		// TODO Auto-generated method stub
		/*VideoExample example = new VideoExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andCourseIdEqualTo(courseId);*/
		
		
		return videoMapper.selectVideoSpeakerByCourseId(courseId);
	}
	
   
}
